package com.hgd.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.hgd.client.device.DeviceImplDelegate;
import com.hgd.client.device.DeviceImplService;
import com.hgd.client.ms.Device;
import com.hgd.client.ms.MSImplDelegate;
import com.hgd.client.ms.MSImplService;
import com.hgd.client.ms.Task;
import com.hgd.client.task.TaskImplDelegate;
import com.hgd.client.task.TaskImplService;
import com.hgd.client.task.TaskKind;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class TaskAction extends ActionSupport {
	private Integer taskKind;
	private Integer deviceId;
	private int taskId;
	private boolean dispatchResult = false;
	private List<Device> deviceList = new ArrayList<Device>();
	//private List<Task> taskList = new ArrayList<Task>();
	private List<TaskKind> kindList = new ArrayList<TaskKind>();
	TaskImplService taskImplService = new TaskImplService();
	TaskImplDelegate taskImplDelegate = taskImplService.getTaskImplPort();
	
	private MSImplService msImplService = new MSImplService();
	private MSImplDelegate msImplDelegate = msImplService.getMSImplPort();
	
	//private DeviceImplService deviceImplService = new DeviceImplService();
	//private DeviceImplDelegate deviceImplDelegate = deviceImplService.getDeviceImplPort();
	
	ActionContext context = ActionContext.getContext(); 
	Map params = context.getParameters(); 
	/**
	 * 任务派遣
	 * @return
	 * @throws Exception
	 */
	public String dispatchTask() throws Exception{
		 dispatchResult = taskImplDelegate.dispatchTask(taskKind, deviceId);
		return SUCCESS;
	}
	/**
	 * ɾ������
	 * @return
	 * @throws Exception
	 */
	public String TaskDel() throws Exception{
		int flag = 0;
		String delResult;
		flag = taskImplDelegate.taskDel(taskId);
		if (flag==1) {
			delResult = SUCCESS;
		}else{
			delResult = ERROR;
		}
		return delResult;
	}
	/**
	 * 任务监控一览表
	 * @return
	 * @throws Exception
	 */
	public String TaskMonitoring() throws Exception{
		params = context.getParameters(); 
		String[] id=(String[])params.get("MSId");
		int msId = Integer.parseInt(id[0]);
		List<Task> taskList = new ArrayList<Task>();
		kindList = taskImplDelegate.getForeverTaskKind();
		deviceList = msImplDelegate.getDeviceByMS(msId);
		for (Iterator iterator = deviceList.iterator(); iterator.hasNext();) {
			Device device = (Device) iterator.next();
			taskList = msImplDelegate.getMSTaskByDid(device.getDId());
			for (Iterator iterator2 = taskList.iterator(); iterator2.hasNext();) {
				Task task = (Task) iterator2.next();
				if (task.getTaskKind().getTType()==1) {
					//taskList.remove(task);
					device.getTasks().add(task);
				}
//				for (Iterator iterator3 = kindList.iterator(); iterator3
//						.hasNext();) {
//					TaskKind taskKind = (TaskKind) iterator3.next();
//					for (Iterator iterator4 = taskList.iterator(); iterator4
//							.hasNext();) {
//						Task task2 = (Task) iterator4.next();
//						if (dispatchResult) {
//							
//						}
//					}
//				}
			}		
		}
		//kindList = taskImplDelegate.getTaskKind();
		return SUCCESS;
	}
	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getAllTask() throws Exception{
		 
		return SUCCESS;
	}

	public Integer getTaskKind() {
		return taskKind;
	}



	public void setTaskKind(Integer taskKind) {
		this.taskKind = taskKind;
	}



	public Integer getDeviceId() {
		return deviceId;
	}



	public void setDeviceId(Integer deviceId) {
		this.deviceId = deviceId;
	}



	public boolean isDispatchResult() {
		return dispatchResult;
	}

	public void setDispatchResult(boolean dispatchResult) {
		this.dispatchResult = dispatchResult;
	}
	public int getTaskId() {
		return taskId;
	}
	public void setTaskId(int taskId) {
		this.taskId = taskId;
	}
	public List<Device> getDeviceList() {
		return deviceList;
	}
	public void setDeviceList(List<Device> deviceList) {
		this.deviceList = deviceList;
	}
	public List<TaskKind> getKindList() {
		return kindList;
	}
	public void setKindList(List<TaskKind> kindList) {
		this.kindList = kindList;
	}
}
